/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of Hewlett-Packard Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2013 All rights reserved. =============================
 */

package com.hp.ecs.sonar;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

import com.hp.ecs.sonar.util.IoUtil;

public class SplitText {

	private static final String UTF_8 = "utf-8";

	@Test
	public void test() {
		String content = "1111111111111111111asdfasdasdafsd";
		int index = content.indexOf('a', 7);
		System.out.println(index);
	}

	public static final int LENGTH = 10000;

	public static String folder;

	public static void main(String[] args) throws Exception {
		split("C:/books/jk/jk.txt");
	}

	private static void split(String filename) throws UnsupportedEncodingException {
		File file = new File(filename);
		folder = file.getParentFile().getAbsolutePath();
		byte[] bytes = IoUtil.read(file);
		String content = new String(bytes, UTF_8);
		System.out.println(content.length());
		for (int i = 100;; i++) {
			int index = content.indexOf('\n', LENGTH);
			write(index > 0 ? content.substring(0, index) : content, i);
			if (index > 0) {
				content = content.substring(index + 1);
			} else {
				break;
			}
		}

	}

	private static void write(String s, int i) throws UnsupportedEncodingException {
		IoUtil.write(("----" + i + "----\n" + s+"----" + i + "完----\n").getBytes("GBK"), folder + "/" + i + ".txt");
	}
}
